// CVideoSourcePage.cpp : implementation file
//

#include "stdafx.h"
#include "avcap.h"
#include "CVideoSourcePage.h"

#include "CLiveCapture.h"
#include "CLiveInputFilters.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CVideoSourcePage dialog


CVideoSourcePage::CVideoSourcePage(CWnd* pParent /*=NULL*/)
	: CDialog(CVideoSourcePage::IDD, pParent)
{
	//{{AFX_DATA_INIT(CVideoSourcePage)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	mLiveCapture  = NULL;
	mInputFilters = NULL;
}

void CVideoSourcePage::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CVideoSourcePage)
	DDX_Control(pDX, IDC_COMBO_VIDEO_CONNECTOR, mComboConnectors);
	DDX_Control(pDX, IDC_RADIO_PAL, mRadioPal);
	DDX_Control(pDX, IDC_RADIO_NTSC, mRadioNtsc);
	DDX_Control(pDX, IDC_RADIO_SECAM, mRadioSecam);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CVideoSourcePage, CDialog)
	//{{AFX_MSG_MAP(CVideoSourcePage)
	ON_BN_CLICKED(IDC_BUTTON_CAPTURE_PROP, OnButtonCaptureProp)
	ON_BN_CLICKED(IDC_BUTTON_TV_PROP, OnButtonTvProp)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVideoSourcePage message handlers
void CVideoSourcePage::SetLiveCapture(CLiveCapture * inData)
{
	mLiveCapture = inData;
}

void CVideoSourcePage::SetInputFilters(CLiveInputFilters * inFilters)
{
	mInputFilters = inFilters;
}

BOOL CVideoSourcePage::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	ASSERT(mLiveCapture);

	FillResolution();
	FillConnectors();

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CVideoSourcePage::FillConnectors(void)
{
	mComboConnectors.ResetContent();

	if (mInputFilters)
	{
		long physicalType  = 0;
		long selectedIndex = 0;
		long count = mInputFilters->GetVideoConnectorCount();
		for (long i = 0; i < count; i++)
		{
			CString connector = mInputFilters->GetVideoConnectorName(i);
			mComboConnectors.AddString(connector);
			// Save physical type of the connector to ComboBox item data
			physicalType = mInputFilters->GetVideoConnectorType(i);
			mComboConnectors.SetItemData(i, physicalType);
			if (mLiveCapture->GetVideoConnector() == physicalType)
			{
				selectedIndex = i;
			}
		}

		if (count)
		{
			mComboConnectors.SetCurSel(selectedIndex);
		}
		else
		{
			mComboConnectors.EnableWindow(FALSE);
		}
	}
}

void CVideoSourcePage::FillResolution(void)
{
	switch (mLiveCapture->GetVideoResolution())
	{
	case SR_NTSC:
		mRadioNtsc.SetCheck(TRUE);
		break;

	case SR_SECAM:
		mRadioSecam.SetCheck(TRUE);
		break;

	case SR_PAL:
	default:
		mRadioPal.SetCheck(TRUE);
		break;
	}
}

void CVideoSourcePage::OnOK() 
{
	EnterConnector();
	EnterResolution();
	
	CDialog::OnOK();
}

void CVideoSourcePage::EnterConnector(void)
{
	int index = mComboConnectors.GetCurSel();
	if (index != CB_ERR)
	{
		long physicalType = mComboConnectors.GetItemData(index);
		mLiveCapture->SetVideoConnector(physicalType);
	}
}

void CVideoSourcePage::EnterResolution(void)
{
	Signal_Resolution resolution = SR_PAL;
	if (mRadioNtsc.GetCheck())
	{
		resolution = SR_NTSC;
	}
	else if (mRadioSecam.GetCheck())
	{
		resolution = SR_SECAM;
	}

	mLiveCapture->SetVideoResolution(resolution);
}

void CVideoSourcePage::OnButtonCaptureProp() 
{
	if (mInputFilters)
	{
		mInputFilters->ShowVideoCapturePropertyPage();
	}
}

void CVideoSourcePage::OnButtonTvProp() 
{
	if (mInputFilters)
	{
		mInputFilters->ShowTVTunerPropertyPage();
	}
}
